package ing.gzq.controller;

import ing.gzq.base.Result;
import ing.gzq.base.ResultCache;
import ing.gzq.model.Answer;
import ing.gzq.service.CourseService;
import ing.gzq.service.QuestionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static ing.gzq.base.ResultCache.getFailureDetail;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    CourseService courseService;

    @Autowired
    QuestionService questionService;

    @RequestMapping(value = "/search/{keyword}", method = RequestMethod.GET)
    public Result search(@PathVariable String keyword) {
        if (StringUtils.isEmpty(keyword)) {
            return ResultCache.getFailureDetail("keyword不能为空");
        }
        return courseService.search(keyword);
    }

    @RequestMapping(value = "/course/join/{courseId}", method = RequestMethod.POST)
    public Result join(@PathVariable Long courseId) {
        return courseService.joinCourse(courseId);
    }

    @RequestMapping(value = "/course/quit/{courseId}", method = RequestMethod.POST)
    public Result quitCourse(@PathVariable Long courseId) {
        return courseService.quitCourse(courseId);
    }

    @RequestMapping(value = "/checkIn/{courseId}", method = RequestMethod.POST)
    public Result sign(@PathVariable Long courseId) {
        return courseService.sign(courseId);
    }
    
    @RequestMapping(value = "/course/all", method = RequestMethod.GET)
    public Result getAllCourse() {
        return courseService.getAllCourse();
    }

    @RequestMapping(value = "/question/{courseId}", method = RequestMethod.GET)
    public Result getQuestionToday(@PathVariable Long courseId) {
        return questionService.getQuestionByStudent(courseId);
    }

    @RequestMapping(value = "/answer/{questionId}", method = RequestMethod.POST)
    public Result answer(@PathVariable Long questionId, Answer answer) {
        if(answer == null){
            return getFailureDetail("answer can not be null");
        }
        answer.setQuestionId(questionId);
        return questionService.uploadAnswer(answer);
    }
}
